package com.zhuxulin.tab.bluetooth;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.widget.Toast;

import com.zhuxulin.tab.App;
import com.zhuxulin.tab.utils.LogUtils;

public class SendMessage implements ISendMsg {
private static final String TAG = SendMessage.class.getSimpleName();
	private App app=null;
	private OutputStream outputStream=null;
	private InputStream inputStream=null;
	public SendMessage(App app) {
		this.app=app;
		outputStream=app.getOutputStream();
		LogUtils.i(TAG, TAG+" getOutputStream:"+outputStream.toString());
		inputStream=app.getInputStream();
		LogUtils.i(TAG, TAG+" getInputStream:"+inputStream.toString());
	}

	@Override
	public boolean sendMessage(String msg) {
		sendMessage(msg.getBytes());
		return true;
	}

	@Override
	public boolean sendMessage(byte[] buffer) {
		if (outputStream == null) {
			LogUtils.i(TAG,
					"Out put stream is null at sendMessage(byte[] buffer)");
			Toast.makeText(app, "Piano may not connected!",
					Toast.LENGTH_LONG).show();
			return false;
		}
		try {
			outputStream.write(buffer);
		} catch (IOException e) {
			LogUtils.i(TAG,
					"send message thorw exception at sendMessage(byte[] msg)");
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean sendMessage(int oneByte) {
		if (outputStream == null) {
			LogUtils.i(TAG,
					"Out put stream is null at sendMessage(int oneByte)");
			Toast.makeText(app, "Piano may not connected!",
					Toast.LENGTH_LONG).show();
			return false;
		}
		try {
			outputStream.write(oneByte);
			return true;
		} catch (IOException e) {
			LogUtils.i(TAG,
					"send message throw exception at sendMessage(int oneByte)");
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean sendMessage(int[] bytes) {
		LogUtils.i(TAG, "Begin send array of int message at sendMessage(int[] bytes)");
		for(int i=0;i<bytes.length;i++){
			if(!sendMessage(bytes[i])){
				return false;
			}
		}
		/**
		boolean isSend=true;
		for(int i=0;i<bytes.length;i++){
			isSend=sendMessage(bytes[i]);
			if(!isSend){
				return false;
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		*/
		LogUtils.i(TAG, "End send array of int message at sendMessage(int[] bytes)");
		return true;
	}

	@Override
	public boolean sendMessage(byte[] buffer, int offset, int len) {
		if (outputStream == null) {
			LogUtils.i(TAG,
					"Out put stream is null at sendMessage(int oneByte)");
			Toast.makeText(app, "Piano may not connected!",
					Toast.LENGTH_LONG).show();
			return false;
		}
		try {
			outputStream.write(buffer, offset, len);
			return true;
		} catch (IOException e) {
			LogUtils.i(
					TAG,
					"send message throw exception at sendMessage(byte[] buffer, int offset, int len)");
			e.printStackTrace();
			return false;
		}
	}

}
